import Vue from 'vue'
import App from './App.vue'
import layer from 'vue-layer'
import 'vue-layer/lib/vue-layer.css'
import 'vue-awesome/icons'
import Icon from 'vue-awesome/components/Icon'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from './router/index.js'
import { Message } from 'element-ui';
import NProgress from 'nprogress' 
import 'nprogress/nprogress.css'// nprogress样式文件
import axios from 'axios'

import VueClipboard from 'vue-clipboard2'
import JsonExcel from 'vue-json-excel'

Vue.component('downloadExcel', JsonExcel)
Vue.use(VueClipboard)

Vue.use(ElementUI);
Vue.component('icon',Icon)
 
Vue.prototype.$layer = layer(Vue);

Vue.prototype.base_url = "http://www.nrunning.com/";

//封装公共AJAX函数
Vue.prototype.ajaxs = (url, obj) => {
	var data = obj.data ? obj.data : {};
	axios({
		method: "POST",
		url: '/admin/' + url,
		data: data,
		headers: {
			'Content-Type': 'application/json',
			'x-requested-with': 'XMLHttpRequest'
		}
	})
		.then(res => {
      res = res.data;
      //登录失效
      if(res.code == 4) {
        location.reload();
        return false;
      }
			obj.success(res)
		})
		.catch(err => {
			obj.error && obj.error();
			console.log(err);
		}).catch(e => {
			ElementUI.Message.error('网络错误');
		})
};


/**
 * 封装layer iframe弹窗样式
 * components string 自定义组件名称
 * area array 弹窗宽高比例[x,y]
 */
Vue.prototype.$iframe = (components,area = ["50%", "calc(100% - 200px)"]) => {
  layer(Vue).open({
    type: 2,
    area: area,
    fixed: false, //不固定
    maxmin: true,
    content: {
      content: components, //传递的组件对象
      parent: this, //当前的vue对象
      data: {} //props
    }
  });
};

Vue.config.productionTip = false
//阿里OSS对象储存域名
Vue.prototype.$oss_url = 'https://ykp-new.oss-cn-hangzhou.aliyuncs.com/';
//封装网页弹窗
Vue.prototype.toast = (code,msg,func = ()=>{}) => {
  if(code == 1) {
    Message.success(msg);
  } 
  if(code == 0) {
    Message.warning(msg);
  }
  setTimeout(func,1000);
}
NProgress.configure({     
  easing: 'linear',  // 动画方式    
  speed: 500,  // 递增进度条的速度    
  showSpinner: false, // 是否显示加载ico    
  trickleSpeed: 200, // 自动递增间隔    
  minimum: 0.3 // 初始化时的最小百分比
})
//当路由开始跳转时
router.beforeEach((to, from , next) => {
  // 开启进度条
  NProgress.start();
  // 这个一定要加，没有next()页面不会跳转的。这部分还不清楚的去翻一下官网就明白了
  next();
});
//当路由跳转结束后
router.afterEach(() => {  
  // 关闭进度条
  NProgress.done()
})

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
